<template>
  <Dialog v-model="dialogVisible" title="套餐余额" width="70%" class="enhanced-dialog">
    <!-- 弹窗头部 -->
    <div class="dialog-header">
      <div class="header-icon">
        <Icon icon="ep:wallet" />
      </div>
      <div class="header-content">
        <h4 class="dialog-title">套餐余额</h4>
        <p class="dialog-subtitle">查看套餐使用情况和余额详情</p>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="content-container">
      <!-- 标签页 -->
      <div class="tabs-container">
        <el-tabs
          v-model="queryParams.productCode"
          tab-position="top"
          @tab-change="filterTables"
          class="enhanced-tabs"
        >
          <el-tab-pane label="点呼话费" name="simple-call">
            <template #label>
              <div class="tab-label">
                <Icon icon="ep:phone" class="tab-icon" />
                <span>点呼话费</span>
              </div>
            </template>
          </el-tab-pane>
          <el-tab-pane label="双向回拨话费" name="double-call">
            <template #label>
              <div class="tab-label">
                <Icon icon="ep:connection" class="tab-icon" />
                <span>双向回拨话费</span>
              </div>
            </template>
          </el-tab-pane>
        </el-tabs>
      </div>

      <!-- 表格区域 -->
      <div class="table-container">
        <el-table
          :data="list"
          class="enhanced-table"
          :header-cell-style="{ background: '#f8f9ff', color: '#303133', fontWeight: '600' }"
        >
          <el-table-column label="套餐名称" align="center" prop="productName" min-width="200">
            <template #default="scope">
              <div class="package-name-cell">
                <Icon icon="ep:box" class="cell-icon" />
                <span class="cell-text">{{ scope.row.productName }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="购买时间"
            align="center"
            prop="createTime"
            :formatter="dateFormatter"
            min-width="210"
          >
            <template #default="scope">
              <div class="time-cell">
                <Icon icon="ep:calendar" class="cell-icon" />
                <span class="cell-text">{{
                  dateFormatter(scope.row, null, scope.row.createTime)
                }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="总量(分钟)" align="center" prop="totalNum" width="160">
            <template #default="scope">
              <div class="number-cell total">
                <Icon icon="ep:pie-chart" class="cell-icon" />
                <span class="cell-text">{{ scope.row.totalNum }}</span>
                <span class="unit">分钟</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="已用(分钟)" align="center" prop="usedNum" width="160">
            <template #default="scope">
              <div class="number-cell used">
                <Icon icon="ep:minus" class="cell-icon" />
                <span class="cell-text">{{ scope.row.usedNum }}</span>
                <span class="unit">分钟</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="剩余(分钟)" align="center" prop="remainNum" width="160">
            <template #default="scope">
              <div class="number-cell remain">
                <Icon icon="ep:plus" class="cell-icon" />
                <span class="cell-text">{{ scope.row.remainNum }}</span>
                <span class="unit">分钟</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="到期时间"
            align="center"
            prop="expireAt"
            :formatter="dateFormatter"
            width="220"
          >
            <template #default="scope">
              <div class="expire-cell">
                <Icon icon="ep:clock" class="cell-icon" />
                <span class="cell-text">{{
                  dateFormatter(scope.row, null, scope.row.expireAt)
                }}</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页区域 -->
      <div class="pagination-container">
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </Dialog>
</template>

<script setup>
import * as AgentApi from '@/api/call/agent'
import { dateFormatter } from '@/utils/formatTime'

const dialogVisible = ref(false)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  productCode: 'simple-call',
  tenantId: null,
  ownerUserId: null
})

const list = ref([])
const total = ref(0)
const isAdmin = ref(false)
/*列表*/
const getList = async () => {
  let data
  if (isAdmin.value) {
    data = await AgentApi.tenantProductPageAdmin(queryParams)
  } else {
    data = await AgentApi.tenantProductPage(queryParams)
  }
  list.value = data.list
  total.value = data.total
}
/*显示*/
const open = (userId, adminId, val) => {
  dialogVisible.value = true
  isAdmin.value = val
  queryParams.pageNo = 1
  queryParams.pageSize = 10
  queryParams.tenantId = adminId
  queryParams.ownerUserId = userId
  queryParams.productCode = 'simple-call'
  getList()
}
/*切换tab*/
const filterTables = () => {
  getList()
}
defineExpose({ open })
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

/* 弹窗头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 内容容器 */
.content-container {
  background: white;
  padding-bottom: 60px;

  @media (max-width: 768px) {
    padding-bottom: 40px;
  }
}

/* 标签页容器 */
.tabs-container {
  padding: 20px 24px 0;
  background: white;

  @media (max-width: 768px) {
    padding: 16px 20px 0;
  }
}

.enhanced-tabs {
  :deep(.el-tabs__header) {
    margin-bottom: 20px;

    @media (max-width: 768px) {
      margin-bottom: 16px;
    }
  }

  :deep(.el-tabs__nav-wrap) {
    &::after {
      height: 2px;
      background-color: $border-light;
    }
  }

  :deep(.el-tabs__item) {
    font-size: 16px;
    font-weight: 600;
    color: $text-regular;
    padding: 12px 20px;
    transition: all 0.3s ease;

    &.is-active {
      color: $primary-color;
    }

    &:hover {
      color: $primary-color;
    }

    @media (max-width: 768px) {
      font-size: 14px;
      padding: 10px 16px;
    }
  }

  :deep(.el-tabs__active-bar) {
    background-color: $primary-color;
    height: 3px;
  }
}

.tab-label {
  display: flex;
  align-items: center;
  gap: 6px;

  @media (max-width: 768px) {
    gap: 4px;
  }
}

.tab-icon {
  font-size: 16px;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

/* 表格容器 */
.table-container {
  padding: 0 24px;
  background: white;
  overflow-x: auto;

  @media (max-width: 768px) {
    padding: 0 20px;
  }
}

.enhanced-table {
  :deep(.el-table__header) {
    th {
      background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%) !important;
      color: $text-primary !important;
      font-weight: 600 !important;
      border-bottom: 2px solid $primary-light;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light !important;
      }

      td {
        padding: 14px 12px;
        border-bottom: 1px solid $border-light;
      }
    }

    .el-table__row--striped {
      background: #fafbff;

      &:hover {
        background: $primary-light !important;
      }
    }
  }

  // H5适配
  @media (max-width: 768px) {
    :deep(.el-table__header) th {
      padding: 12px 8px;
      font-size: 13px;
    }

    :deep(.el-table__body) td {
      padding: 12px 8px;
      font-size: 12px;
    }
  }
}

/* 表格单元格样式 */
.package-name-cell,
.time-cell,
.expire-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.number-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  &.total {
    .cell-icon {
      color: $primary-color;
    }
    .cell-text {
      color: $primary-color;
      font-weight: 600;
    }
  }

  &.used {
    .cell-icon {
      color: $danger-color;
    }
    .cell-text {
      color: $danger-color;
      font-weight: 600;
    }
  }

  &.remain {
    .cell-icon {
      color: $success-color;
    }
    .cell-text {
      color: $success-color;
      font-weight: 600;
    }
  }
}

.cell-icon {
  font-size: 14px;
  flex-shrink: 0;

  &.ep-box {
    color: $primary-color;
  }

  &.ep-calendar,
  &.ep-clock {
    color: $text-secondary;
  }
}

.cell-text {
  color: $text-regular;
  font-weight: 500;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.unit {
  font-size: 11px;
  color: $text-secondary;
  background: rgba(0, 0, 0, 0.05);
  padding: 1px 4px;
  border-radius: 3px;

  @media (max-width: 768px) {
    font-size: 10px;
  }
}

/* 分页容器 */
.pagination-container {
  display: flex;
  justify-content: center;
  padding: 20px 24px;
  background: white;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
  }

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }

      &.is-active {
        background: $primary-color;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }
    }

    @media (max-width: 768px) {
      .el-pagination__sizes,
      .el-pagination__jump {
        display: none;
      }
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .content-container,
  .tabs-container,
  .table-container,
  .pagination-container {
    background: #374151;
  }

  .dialog-title {
    color: #f9fafb;
  }

  .cell-text {
    color: #f9fafb;
  }

  .enhanced-table {
    :deep(.el-table__header) {
      th {
        background: #4b5563 !important;
        color: #f9fafb !important;
        border-bottom-color: #6b7280;
      }
    }

    :deep(.el-table__cell) {
      border-bottom-color: #4b5563;
    }
  }

  .enhanced-tabs {
    :deep(.el-tabs__nav-wrap) {
      &::after {
        background-color: #6b7280;
      }
    }

    :deep(.el-tabs__item) {
      color: #d1d5db;

      &.is-active {
        color: #60a5fa;
      }

      &:hover {
        color: #60a5fa;
      }
    }

    :deep(.el-tabs__active-bar) {
      background-color: #60a5fa;
    }
  }

  .number-cell {
    &.total {
      .cell-icon,
      .cell-text {
        color: #60a5fa;
      }
    }

    &.used {
      .cell-icon,
      .cell-text {
        color: #f87171;
      }
    }

    &.remain {
      .cell-icon,
      .cell-text {
        color: #34d399;
      }
    }
  }

  .unit {
    background: rgba(255, 255, 255, 0.1);
    color: #9ca3af;
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 12px 16px;
  }

  .tabs-container {
    padding: 12px 16px 0;
  }

  .table-container {
    padding: 0 16px;
  }

  .pagination-container {
    padding: 12px 16px;
  }

  .enhanced-table {
    :deep(.el-table__header) th {
      padding: 10px 6px;
      font-size: 12px;
    }

    :deep(.el-table__body) td {
      padding: 10px 6px;
      font-size: 11px;
    }
  }

  .cell-text {
    font-size: 11px;
  }

  .unit {
    font-size: 9px;
  }

  .tab-label {
    gap: 3px;
  }

  .tab-icon {
    font-size: 12px;
  }
}
</style>
